﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

class OptionsScreen : MenuScreen
{
    SpriteFont font;

    private Texture2D backgroundTexture;
    private Vector2 backgroundPosition;

    string okSound;

    MenuEntry goToPreviousEntry, soundVolume, musicVolume, fullScreenToggle, muteToggle;
    
    private Texture2D back, fullOn, fullOff, muteOn, muteOff, sound, music;

    public OptionsScreen()
        : base()
    {
        Direction = MenuEntryDirection.OPTIONSCREEN;

        musicVolume = new MenuEntry("");
        musicVolume.Position = new Vector2(0, 0f);
        MenuEntries.Add(musicVolume);

        soundVolume = new MenuEntry("");
        soundVolume.Position = new Vector2(0, 0f);
        MenuEntries.Add(soundVolume);

        muteToggle = new MenuEntry("");
        muteToggle.Position = new Vector2(0, 0f);
        muteToggle.Selected += OnMute;
        MenuEntries.Add(muteToggle); 

        fullScreenToggle = new MenuEntry("");
        fullScreenToggle.Position = new Vector2(0, 0f);
        fullScreenToggle.Selected += OnFullScreen;
        MenuEntries.Add(fullScreenToggle);

        goToPreviousEntry = new MenuEntry("");
        goToPreviousEntry.Position = new Vector2(0, 0f);
        goToPreviousEntry.Selected += OnCancel;
        MenuEntries.Add(goToPreviousEntry);    
    }


    /// <summary>
    /// Load the graphics content for this screen.
    /// </summary>
    public override void LoadContent()
    {
        ContentManager content = ScreenManager.Game.Content;

        okSound = @"Audio\m_ok";

        font = content.Load<SpriteFont>("SpriteFont1");

        backgroundTexture = content.Load<Texture2D>(@"Textures\MenuMock\options fundo");
        
        Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
        backgroundPosition = new Vector2(
            (viewport.Width - backgroundTexture.Width) / 2,
            (viewport.Height - backgroundTexture.Height) / 2);


        fullOn = content.Load<Texture2D>(@"Textures\MenuMock\op fullscreen on");
        fullOff = content.Load<Texture2D>(@"Textures\MenuMock\op fullscreen off"); 

        muteOn = content.Load<Texture2D>(@"Textures\MenuMock\op mute on");
        muteOff = content.Load<Texture2D>(@"Textures\MenuMock\op mute off");


        goToPreviousEntry.SelectedTexture = content.Load<Texture2D>(@"Textures\MenuMock\op back");
        soundVolume.SelectedTexture = content.Load<Texture2D>(@"Textures\MenuMock\op sounds");
        musicVolume.SelectedTexture = content.Load<Texture2D>(@"Textures\MenuMock\op music");
        fullScreenToggle.SelectedTexture = fullOff;
        muteToggle.SelectedTexture = muteOff;

        base.LoadContent();
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            ExitScreen();
            return;
        }

        base.HandleInput();
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        // draw the background images
        spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);

        // Draw each menu entry in turn.
        for (int i = 0; i < MenuEntries.Count; i++)
        {
            MenuEntry menuEntry = MenuEntries[i];
            bool isSelected = IsActive && (i == selectedEntry);
            menuEntry.Draw(this, isSelected, gameTime);
        }

        spriteBatch.End();
    }

    protected void OnFullScreen(object sender, EventArgs e)
    {
        Globals.FullScreen = !Globals.FullScreen;

        if (Globals.FullScreen)
            fullScreenToggle.SelectedTexture = fullOn;
        else
            fullScreenToggle.SelectedTexture = fullOff;

    }

    protected void OnMute(object sender, EventArgs e)
    {
        Globals.Mute = !Globals.Mute;

        if (Globals.Mute)
            muteToggle.SelectedTexture = muteOn;
        else
            muteToggle.SelectedTexture = muteOff;
    }
}
